package components.intertitles;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Timer;

import menus.common.AbstractMenu;
import menus.demm.DEMM;
import menus.flower.FlowerMenu;
import menus.zone.ZoneMenu;

import components.SetupExperiment;
import components.factors.Technique.TechniqueInterface;

import fr.inria.insitu.noe.Platform;
import fr.inria.insitu.noe.exp.model.Experiment;
import fr.inria.insitu.noe.exp.model.Intertitle;
import fr.lri.swingstates.canvas.CRectangle;
import fr.lri.swingstates.canvas.CShape;
import fr.lri.swingstates.canvas.CStateMachine;
import fr.lri.swingstates.canvas.Canvas;

public class BlockInstructions extends Intertitle {
	
	public static final String GoOnTag = "goOn";
	public static final String ThanksTag = "thanks";
	public static final String InstructionTag = "InstructionsBlock";
	public static final String InstructionsTimer = "InstructionsTimer";
	public static final String StartTag = "start";
	public static final String PauseTag = "Pause Tag";
	
	public static final String LastTechnique = "Last Technique";
	public static final String LastCapacity = "Last Capacity";
	public static final String LastTraining = "Last Training";
	
	public static final int PauseGap = 8;
	
	protected int block = 0, nbBlocks;
	protected String technique, sentence0a, sentence0b, sentence1, sentence2a, sentence3, sentence4, sentence5, thisTrain;
	protected static Canvas canvas;
	
	protected CRectangle startButton;
	
	protected MouseAdapter goOnListener;
	
	public static Font font = new Font("verdana", Font.PLAIN, 30);
	
	public BlockInstructions() {
		
		canvas = ((SetupExperiment)Platform.getExperiment().getSetUp()).getGestureArea();
		
		nbBlocks = (Integer)Platform.getInstance().getFactorValue(Experiment.MEASURE_EXPERIMENT_NB_BLOCK);
		
		goOnListener = new MouseAdapter(){
			
			@Override
			public void mousePressed(MouseEvent e) {
				if ( 	// If the user presses on a goOn-tagged object
						canvas.pick(((MouseEvent)e).getPoint()) != null 
						&& canvas.pick(((MouseEvent)e).getPoint()).hasTag(GoOnTag) 
				){
					canvas.removeShapes(canvas.getTag(ThanksTag));
					
					for (Object s : canvas.getTag(InstructionTag).getCollection()){
						((CShape)s).setDrawable(true);
					}
					
					if (thisTrain.contains("Yes"))
						canvas.getTag(Trial.FrameTag).firstShape().setDrawable(true);
				}
			}
			
		};
	}
	
	@Override
	public void beginIntertitle() {
		
		canvas.addMouseListener(goOnListener);
		canvas.requestFocus();
		
		block = (Integer)Platform.getInstance().getMeasureValue(Experiment.MEASURE_EXPERIMENT_BLOCK);
		
		String lastTech = (String)Platform.getInstance().getMeasureValue(LastTechnique);
		String lastCapa = (String)Platform.getInstance().getMeasureValue(LastCapacity);
		String lastTrain = (String)Platform.getInstance().getMeasureValue(LastTraining);
		
		String thisTech = Platform.getInstance().getFactorValue(Trial.Technique).toString();
		String thisCapa = Platform.getInstance().getFactorValue(Trial.Capacity).toString();
		thisTrain = Platform.getInstance().getFactorValue(Trial.Training).toString();
		
		boolean changeSuperBlock = !
			(thisCapa.equalsIgnoreCase(lastCapa)
			&& thisTech.equalsIgnoreCase(lastTech)
			&& thisTrain.equalsIgnoreCase(lastTrain));
		
		System.out.println("BlockInstructions.beginIntertitle()");
		System.out.println("\t- Capacity  : " + lastCapa + " -> " + thisCapa);
		System.out.println("\t- Technique : " + lastTech + " -> " + thisTech);
		System.out.println("\t- Training  : " + lastTrain + " -> " + thisTrain);
		System.out.println("\t==> changeSuperBlock = " + changeSuperBlock);
		
		Platform.getInstance().setMeasureValue(LastTechnique, thisTech);
		Platform.getInstance().setMeasureValue(LastCapacity, thisCapa);
		Platform.getInstance().setMeasureValue(LastTraining, thisTrain);
		
		sentence0a = "Thank you, you've just completed "
			+ (100*(block-1)/nbBlocks)
			+ "% of the experiment.";
		
		sentence0b = "Go on !";
		
		if (thisTrain.contains("No"))
			sentence1 = "Begin block";
		else
			sentence1 = "** Training block **";
		
		if (thisTech.contains("DEMM")){
			technique = "Distance Extended Marking Menu";
		}
		else{
			technique = thisTech + " Menu";
		}
		
		sentence2a = "Select items in a " + technique;
		
		sentence3 = "For this block, you will have to distinguish between " 
			+ thisCapa
			+ " items.";
		
		sentence4 = "You can use as much space as you like in order to be at ease with the relative lengths.";
		
		sentence5 = "Do your selections as quickly and accurately as possible.";
		
		canvas.newText(0, 0, sentence0a, font)
				.setReferencePoint(0.5, 0.5)
				.translateTo(canvas.getWidth()/2, canvas.getHeight()/2 - 200)
				.setPickable(false)
				.addTag(ThanksTag)
				.setDrawable(block > 1 && changeSuperBlock);
		
		canvas.newRectangle(0, 0, 150, 50)
				.setReferencePoint(0.5, 0.5)
				.translateTo(canvas.getWidth()/2, canvas.getHeight()/2 - 80)
				.setPickable(true)
				.addTag(ThanksTag)
				.addTag(GoOnTag)
				.setFilled(true)
				.setFillPaint(Color.blue)
				.setDrawable(block > 1 && changeSuperBlock);
		
		canvas.newText(0, 0, sentence0b, font)
				.setReferencePoint(0.5, 0.5)
				.translateTo(canvas.getWidth()/2, canvas.getHeight()/2 - 80)
				.setPickable(false)
				.setFillPaint(Color.white)
				.addTag(ThanksTag)
				.addTag(GoOnTag)
				.setDrawable(block > 1 && changeSuperBlock);
		
		startButton = (CRectangle)canvas.newRectangle(0, 0, 400, 50)
				.setReferencePoint(0.5, 0.5)
				.translateTo(canvas.getWidth()/2, canvas.getHeight()/2 - 200)
				.setPickable(true)
				.addTag(InstructionTag)
				.addTag(GoOnTag)
				.addTag(StartTag)
				.setFilled(true)
				.setFillPaint(Color.blue)
				.setDrawable(block == 1 || !changeSuperBlock);
		
		// TODO => cf. SetupExperiment
		 Platform.getInstance().setMeasureValue(SetupExperiment.START_BUTTON, startButton);
		
		canvas.newText(0, 0, sentence1, font)
				.setReferencePoint(0.5, 0.5)
				.translateTo(canvas.getWidth()/2, canvas.getHeight()/2 - 200)
				.setPickable(false)
				.setFillPaint(Color.white)
				.addTag(InstructionTag)
				.addTag(StartTag)
				.setDrawable(block == 1);
		
		canvas.newText(0, 0, sentence2a, font)
				.setReferencePoint(0.5, 0.5)
				.translateTo(canvas.getWidth()/2, canvas.getHeight()/2 - 80)
				.setPickable(false)
				.addTag(InstructionTag)
				.setDrawable(block == 1);
		
		canvas.newText(0, 0, sentence3, font)
				.setReferencePoint(0.5, 0.5)
				.translateTo(canvas.getWidth()/2, canvas.getHeight()/2 + 80)
				.setPickable(false)
				.addTag(InstructionTag)
				.setDrawable(block == 1);
		
		canvas.newText(0, 0, sentence4, font)
				.setReferencePoint(0.5, 0.5)
				.translateTo(canvas.getWidth()/2, canvas.getHeight()/2 + 200)
				.setPickable(false)
				.addTag(InstructionTag)
				.setDrawable(block == 1);
		
		canvas.newText(0, 0, sentence5, font)
				.setReferencePoint(0.5, 0.5)
				.translateTo(canvas.getWidth()/2, canvas.getHeight()/2 + 260)
				.setPickable(false)
				.addTag(InstructionTag)
				.setDrawable(block == 1);
		
		// Pauses
		
		canvas.newText(0, 0, "- PAUSE -", font)
			.setReferencePoint(0.5, 0.5)
			.translateTo(canvas.getWidth()/2, canvas.getHeight()/2 - 300)
			.setPickable(false)
			
			.addTag(PauseTag)
			
			.setDrawable(!changeSuperBlock);
		
		canvas.newText(0, 0, "You may have a break. Click on the blue button when you feel ready to continue", font)
			.setReferencePoint(.5, .5)
			.translateTo(canvas.getWidth()/2, canvas.getHeight()/2 - 100)
		
			.addTag(PauseTag)
		
			.setPickable(false)
			.setDrawable(!changeSuperBlock);	
		
	}
	
	@Override
	public void endIntertitle() {
		canvas.removeShapes(canvas.getTag(InstructionTag));
		canvas.removeMouseListener(goOnListener);
		canvas.removeShapes(canvas.getTag(PauseTag));
	}

}
